/**  
 * Project Name:api-webhook  
 * File Name:InsuranceService.java  
 * Package Name:com.example.service.webhook.business.insurance.service  
 * Date:2016年10月29日下午4:24:04  
 * Copyright (c) 2016, yuandong@51dojo.com All Rights Reserved.  
 *  
*/

package com.example.service.webhook.business.insurance.service;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import com.example.service.webhook.business.insurance.api.InsuranceClient;
import com.example.service.webhook.business.insurance.billitem.InsuranceBillTask;
import com.example.service.webhook.business.insurance.dto.InsurancePolicyBaseInfo;
import com.example.service.webhook.business.insurance.dto.InsurancePolicyInfo;
import com.example.service.webhook.business.insurance.dto.VehicleInsuranceDetailInfo;
import com.example.service.webhook.business.insurance.entity.InsurancePolicyBaseInfoEntity;
import com.example.service.webhook.business.insurance.entity.VehicleInsuranceDetailInfoEntity;
import com.example.service.webhook.business.insurance.repository.InsurancePolicyBaseInfoRepository;
import com.example.service.webhook.business.insurance.repository.VehicleInsuranceDetailInfoRepository;
import com.example.service.webhook.business.jingdong.billitem.JdBillTask;
import com.example.service.webhook.util.DateUtil;

/**
 * ClassName:InsuranceService <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2016年10月29日 下午4:24:04 <br/>
 * 
 * @author yuandong
 * @version
 * @since JDK 1.6
 * @see
 */
@Service
public class InsuranceService {

	private static final Logger LOGGER = LoggerFactory.getLogger(InsuranceService.class);

	@Autowired
	private ThreadPoolTaskExecutor taskExecutor;
	@Autowired
	private InsurancePolicyBaseInfoRepository insurancePolicyBaseInfoRepository;
	@Autowired
	private VehicleInsuranceDetailInfoRepository vehicleInsuranceDetailInfoRepository;
	@Autowired
	private InsuranceClient insuranceClient;

	/**
	 * 
	 * fetchBill:处理保单回调 <br/>
	 * 
	 * @author yuandong
	 * @param task
	 * @since JDK 1.6
	 */
	public void fetchBill(final InsuranceBillTask task) {

		// 这里交给线程池处理，防止下面的业务处理时间太长，导致超时。
		// 超时会导致魔蝎数据进行重试，会收到重复的回调请求

		taskExecutor.execute(new Runnable() {
			@Override
			public void run() {
				try {
					Date startTime = DateUtil.getCurrentDate();
					List<InsurancePolicyInfo> insuranceList = insuranceClient.getInsurance(task.getTaskId());
					if(insuranceList!=null&&!insuranceList.isEmpty()){
						for(InsurancePolicyInfo insurancePolicyInfo:insuranceList){
							InsurancePolicyBaseInfo insurancePolicyBaseInfo = insurancePolicyInfo.getInsurancePolicyBaseInfo();
							if(insurancePolicyBaseInfo!=null){
								InsurancePolicyBaseInfoEntity insurancePolicyBaseInfoEntity = new InsurancePolicyBaseInfoEntity();
								BeanUtils.copyProperties(insurancePolicyBaseInfo, insurancePolicyBaseInfoEntity);
								insurancePolicyBaseInfoEntity.setUserId(task.getUserId());
								InsurancePolicyBaseInfoEntity isExist = insurancePolicyBaseInfoRepository.getInsurancePolicyBaseInfo(task.getUserId(), task.getMappingId(), insurancePolicyBaseInfo.getPolicyNum());
								if(isExist!=null){
									insurancePolicyBaseInfoEntity.setId(isExist.getId());
									insurancePolicyBaseInfoEntity.setCreateTime(isExist.getCreateTime());
									insurancePolicyBaseInfoRepository.updateInsurancePolicyBaseInfo(insurancePolicyBaseInfoEntity);
								}else{
									insurancePolicyBaseInfoRepository.saveInsurancePolicyBaseInfo(insurancePolicyBaseInfoEntity);
								}
								
								if(insurancePolicyInfo.getVehicleInsuranceDetailInfos()!=null&&!insurancePolicyInfo.getVehicleInsuranceDetailInfos().isEmpty()){
									vehicleInsuranceDetailInfoRepository.deleteVehicleInsuranceDetailInfo(task.getUserId(), insurancePolicyBaseInfo.getPolicyNum());
									for(VehicleInsuranceDetailInfo vehicleInsuranceDetailInfo:insurancePolicyInfo.getVehicleInsuranceDetailInfos()){
										VehicleInsuranceDetailInfoEntity vehicleInsuranceDetailInfoEntity = new VehicleInsuranceDetailInfoEntity();
										BeanUtils.copyProperties(vehicleInsuranceDetailInfo, vehicleInsuranceDetailInfoEntity);
										vehicleInsuranceDetailInfoEntity.setUserId(task.getUserId());
										vehicleInsuranceDetailInfoRepository.saveVehicleInsuranceDetailInfo(vehicleInsuranceDetailInfoEntity);
									}
								}
							}
							
						}
					}
					
					Long taskTime = DateUtil.distance(startTime, DateUtil.getCurrentDate());
					LOGGER.info(String.format("保单任务耗时 %s,taskId=%s", taskTime.toString(), task.getTaskId()));
				} catch (Exception e) {
					LOGGER.error("insurance callback error,taskId="+task.getTaskId(),e);
				}
				
				

			}
		}

		);

	}

}
